/* 
 * oms，一个基于J2EE架构运营支撑平台
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.publish.impl;

import java.util.List;

import org.grayrabbit.cms.dao.publish.PublishTaskDao;
import org.grayrabbit.cms.entity.PublishTask;
import org.grayrabbit.cms.vo.publish.PublishTaskBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>PublishTask</b> dao implementation 

 * @version 1.0，创建时间：2012-01-05 17:51:15 
 */
@Repository
public class PublishTaskDaoImpl extends GenericHibernateDao<PublishTask, String>
		implements PublishTaskDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From PublishTask as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see PublishTaskDao#assembly(PublishTaskBean)
	 */
	public QueryCondition assembly(PublishTaskBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		trans(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see PublishTaskDao#assemblyCount(PublishTaskBean)
	 */
	public QueryCondition assemblyCount(PublishTaskBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		trans(bean,qc);
		return qc;
	}

	/**
	 * @see PublishTaskDao#count(PublishTaskBean)
	 */
	public int count(PublishTaskBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see PublishTaskDao#assemblyQuery(PublishTaskBean)
	 */
	public QueryCondition assermblyQuery(PublishTaskBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		trans(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see PublishTaskDao#assemblyQueryCount(PublishTaskBean)
	 */
	public QueryCondition assemblyQueryCount(PublishTaskBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		trans(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see PublishTaskDao#findByPage(Page, PublishTaskBean)
	 */
	public List<PublishTask> findByPage(Page<PublishTask> page,
			PublishTaskBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	
	// 业务查询主要逻辑代码
	private void trans(PublishTaskBean bean,QueryCondition qc){
		//TODO:兄弟们此处根据业务修改代码，完成后，请删除本行，沈永生于2012-01-05 17:51:15提醒!
		if(bean!=null){
		            if(bean.getTaskName() != null){
				qc.add(" And obj.taskName like ? ", "%"+bean.getTaskName()+"%");
			}
            if(bean.getTaskBeginTime() != null){
				qc.add(" And obj.taskBeginTime like ? ", "%"+bean.getTaskBeginTime()+"%");
			}
            if(bean.getTaskEndTime() != null){
				qc.add(" And obj.taskEndTime like ? ", "%"+bean.getTaskEndTime()+"%");
			}
            if(bean.getTaskStatus() != null){
				qc.add(" And obj.taskStatus like ? ", "%"+bean.getTaskStatus()+"%");
			}
            if(bean.getIsAutoPublish() != null){
				qc.add(" And obj.isAutoPublish like ? ", "%"+bean.getIsAutoPublish()+"%");
			}
            if(bean.getTaskOperLoginName() != null){
				qc.add(" And obj.taskOperLoginName like ? ", "%"+bean.getTaskOperLoginName()+"%");
			}
            if(bean.getTaskOperRealName() != null){
				qc.add(" And obj.taskOperRealName like ? ", "%"+bean.getTaskOperRealName()+"%");
			}
            if(bean.getCreateCount() != null){
				qc.add(" And obj.createCount like ? ", "%"+bean.getCreateCount()+"%");
			}
            if(bean.getErrorCount() != null){
				qc.add(" And obj.errorCount like ? ", "%"+bean.getErrorCount()+"%");
			}
            if(bean.getPublishConfig() != null){
				qc.add(" And obj.publishConfig like ? ", "%"+bean.getPublishConfig()+"%");
			}

		}
	}

}